package root.electronclass.electronclass.school.rep

import android.util.Log
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.data
import root.electronclass.electronclass.mvp.base.isSuccessful
import root.electronclass.electronclass.mvp.entity.*
import root.electronclass.electronclass.mvp.rest.RestManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**

 */
object SchoolRepository {


    /**
     * 获取校园通知
     */
    suspend fun getSchoolNotification(pageIndex: Int, type: Int, key: String? = ""): List<NoticeData> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().notice(SchoolNoticeDto(0, key, dateType = type,  pageIndex))
        baseResponse.let {
            check(it)
            it.data
        }
    }

    /**
     * 获取校园宣传
     */
    suspend fun campusPublicity(pageIndex: Int, type: Int, key: String? = ""): List<SchoolPropagandaDto> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().campusPublicity(SchoolPropaganda( key, dateType = type,  pageIndex))
        baseResponse.let {
            check(it)
            it.data
        }
    }

    /**
     * 获取意见箱
     */
    suspend fun ideaBox(pageIndex: Int, type: Int, key: String? = ""): List<IdeaBox> = withContext(Dispatchers.IO) {
        val box = IdeaBox()
        box.dateType = type
        box.pageNum = pageIndex
        box.remark = key
        val baseResponse = RestManager.getInstance().initRest().ideaBox(box)
        baseResponse.let {
            check(it)
            it.data
        }
    }


    /**
     * 获取意见箱
     */
    suspend fun learnSpace(pageIndex: Int, type: Int, subjectName: String? = "",remark: String? = ""): List<LearnSpace> = withContext(Dispatchers.IO) {
        val learnSpace = LearnSpace()
        learnSpace.dateType = type
        learnSpace.pageNum = pageIndex
        learnSpace.subjectName = subjectName
        learnSpace.name = remark
        val baseResponse = RestManager.getInstance().initRest().learnSpace(learnSpace)
        baseResponse.let {
            check(it)
            it.data
        }
    }


    /**
     *
     */
    suspend fun updateNotice(item: NoticeData): BaseResponse<Any> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().updateNotice(item)
        baseResponse.let {
           it
        }
    }

    suspend fun rcHonorList(): BaseResponse<List<RcHonor>> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().rcHonorList()
        baseResponse.let {
            it
        }
    }

    suspend fun getRcHonorList(vo: RcHonorVo): BaseResponse<List<HonorList>> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().getRcHonorList(vo)
        baseResponse.let {
            it
        }
    }

    suspend fun listByFirstChar(option: String?): BaseResponse<List<FirstCharTeacherDto>> = withContext(Dispatchers.IO) {
        val baseResponse = RestManager.getInstance().initRest().listByFirstChar(FirstCharQueryDTO(option))
        baseResponse.let {
            it
        }
    }


    /**
     *
     */
    suspend fun message(item: MessageDto): List<Message> = withContext(Dispatchers.IO) {
        Log.i("","未读消息：${item}")
        val baseResponse = RestManager.getInstance().initRest().message(item)
        baseResponse.let {
            check(it)
            it.rows.list
        }
    }

    private fun <T> check(it: BaseResponse<T>?) {
        if (!it.isSuccessful && it != null) {
            throw ApiException(it.code, it.msg)
        }
    }
}